﻿var manager = new breeze.EntityManager('/Breeze/Data');
$(function () {
    vm.Role($.trim($('#_role').text()));


});


var vm = {
    Role: ko.observable()
}

function loadMenu() {
    if ($('#_accid').text() != "") {
        var query = breeze.EntityQuery
                  .from('Accounts')
                  .where('UniqueId', '==', $.trim($('#_accid').text()));
        manager.executeQuery(query).then(function (data) {
            data.results.forEach(function (item) {
                var html = '';
                for (var i = 0; i < item.Role().RoleMenu().length; i++) {
                    html += "<li><a href=/" + item.Role().RoleMenu()[i].Menu().Controller() + "/" + item.Role().RoleMenu()[i].Menu().Method() + ">" + item.Role().RoleMenu()[i].Menu().Name() + "</a></li>";
                }
                $('#menu').append(html);
                ko.cleanNode($('#welcome')[0]);
                ko.applyBindings(vm, $('#welcome')[0]);
            });
        }).fail(function (e) {
            alert(e);
        });
    }
    

}
function saveFailed(err) {
    alert(err);
}
function saveSucceeded() {
    alert("Successful");
}
function saveSingleEntity(entity, arrEntity) {
    if (entity != null) {
        manager.saveChanges([entity]).then(saveSucceeded).fail(saveFailed);
    } else {
        manager.saveChanges(arrEntity).then(saveSucceeded).fail(saveFailed);
    }
    
}
function queuingSave(EntityArray1, EntityArray2, EntityArray3) {
    if (EntityArray1 != null && $.isArray(EntityArray1)) {
        manager.saveChanges(EntityArray1).then(function () {
            if (EntityArray2 != null && $.isArray(EntityArray2)) {
                manager.saveChanges(EntityArray2).then(function () {
                    if (EntityArray3 != null && $.isArray(EntityArray3)) {
                        manager.saveChanges(EntityArray3).then(saveSucceeded).fail(saveFailed);
                    } else saveSucceeded();
                }).fail(saveFailed);
            } else saveSucceeded();
        }).fail(saveFailed);
    }
}

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}
function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}
function addPagination(maxPage) {
    $('#paginator').html('');
    var html = '';
    html += "<!-- ko if: pagenumber() != 1 -->";
    html += "<li><a href='javascript:void(0)' data-bind='click: vm.firstPage'>First</a></li>";
    html += "<li><a href='javascript:void(0)' data-bind='click: vm.prevPage'>&laquo;</a></li>";
    html += "<!-- /ko -->";

    html += "<li><span>page <input style=' width: 50px' data-bind='numeric: pagenumber, value: pagenumber' /> /" + maxPage + " </span></li>";

    html += "<!-- ko if: pagenumber() != maxPage() -->";
    html += "<li><a href='javascript:void(0)' data-bind='click: vm.nextPage'>&raquo;</a></li>";
    html += "<li><a href='javascript:void(0)' data-bind='click: vm.lastPage'>Last</a></li>";
    html += "<!-- /ko -->";
    $('#paginator').append(html);

    ko.cleanNode($('#paginator')[0]);
    ko.applyBindings(vm, $('#paginator')[0]);
}

ko.bindingHandlers.numeric = {
    init: function (element, valueAccessor) {
        $(element).on("keydown", function (event) {
            // Allow: backspace, delete, tab, escape, and enter
            if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
                // Allow: Ctrl+A
                (event.keyCode == 65 && event.ctrlKey === true) ||
                // Allow: . ,
                (event.keyCode == 188 || event.keyCode == 190 || event.keyCode == 110) ||
                // Allow: home, end, left, right
                (event.keyCode >= 35 && event.keyCode <= 39)) {
                // let it happen, don't do anything
                return;
            }
            else {
                // Ensure that it is a number and stop the keypress
                if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
                    event.preventDefault();
                }
            }
        });
    }
};


